{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hegwDOfffwzw",
        "outputId": "1e221210-152b-4f5b-f009-9b9ffec2fa9f"
      },
      "outputs": [],
      "source": [
        "!rm -rf /content/ChatTTS\n",
        "!git clone https://github.com/2noise/ChatTTS.git\n",
        "!pip install -r /content/ChatTTS/requirements.txt\n",
        "!pip install nemo_text_processing WeTextProcessing\n",
        "!ldconfig /usr/lib64-nvidia"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "lDSQ6Xf-bSre"
      },
      "outputs": [],
      "source": [
        "from dotenv import load_dotenv\n",
        "load_dotenv(\"sha256.env\")\n",
        "\n",
        "import torch\n",
        "torch._dynamo.config.cache_size_limit = 64\n",
        "torch._dynamo.config.suppress_errors = True\n",
        "torch.set_float32_matmul_precision('high')\n",
        "\n",
        "from ChatTTS import ChatTTS\n",
        "from IPython.display import Audio"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vBzG5gxcbSrf"
      },
      "source": [
        "## Load Models"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 49,
          "referenced_widgets": [
            "c365a95346ec4b09a1e6467bf313baf7",
            "d79fd51849fd463cb08b83fdb8e5ca0c",
            "d247683a0a61441b971dfb39062e1fbf",
            "1da23fc236034f32adcaf6bb2e0e7d80",
            "4b2126d97c514795ab2a90f7357a203c",
            "9775ce64008b417fac3edd55b9e999d9",
            "96c9bb2eff4043b2a5dbd1e3e65375e5",
            "20aa0031b7bb45bf82443b48b3694166",
            "67252ea545d64392a1bd6ac40852e65f",
            "2f920c00bcac4787a0078ee035e97b43",
            "ba592297ff5347aebae298770a29fb8c"
          ]
        },
        "id": "e0QSkngRbSrg",
        "outputId": "138ac28b-6a33-4c31-8fe3-8481bb213d02"
      },
      "outputs": [],
      "source": [
        "chat = ChatTTS.Chat()\n",
        "\n",
        "# Use force_redownload=True if the weights updated.\n",
        "chat.load_models(force_redownload=True)\n",
        "\n",
        "# If you download the weights manually, set source='locals'.\n",
        "# chat.load_models(source='local', local_path='YOUR LOCAL PATH')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bAUs0rGQbSrh"
      },
      "source": [
        "## Inference"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NPZ2SFksbSrh"
      },
      "source": [
        "### Batch infer"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Su9FmUYAbSrh",
        "outputId": "7c2aa0c1-1f99-4da1-b2e5-bbcb93465d89"
      },
      "outputs": [],
      "source": [
        "texts = [\"So we found being competitive and collaborative was a huge way of staying motivated towards our goals, so one person to call when you fall off, one person who gets you back on then one person to actually do the activity with.\",]*3 \\\n",
        "        + [\"我觉得像我们这些写程序的人，他，我觉得多多少少可能会对开源有一种情怀在吧我觉得开源是一个很好的形式。现在其实最先进的技术掌握在一些公司的手里的话，就他们并不会轻易的开放给所有的人用。\"]*3\n",
        "\n",
        "wavs = chat.infer(texts)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 76
        },
        "id": "YQRwB8lpbSri",
        "outputId": "62ca9282-2755-44a5-ffca-c05c5e35ce76"
      },
      "outputs": [],
      "source": [
        "Audio(wavs[0], rate=24_000, autoplay=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 76
        },
        "id": "LuFG6m7AbSri",
        "outputId": "d8e0e3a2-d9fe-44db-e1f4-e2596289270e"
      },
      "outputs": [],
      "source": [
        "Audio(wavs[3], rate=24_000, autoplay=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oLhAGvkfbSrj"
      },
      "source": [
        "### Custom params"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kma0HBEBbSrj",
        "outputId": "b80b9d2f-8248-41ee-f1d7-eb3bf331ee69"
      },
      "outputs": [],
      "source": [
        "params_infer_code = {'prompt':'[speed_5]', 'temperature':.3}\n",
        "params_refine_text = {'prompt':'[oral_2][laugh_0][break_6]'}\n",
        "\n",
        "wav = chat.infer('四川美食可多了，有麻辣火锅、宫保鸡丁、麻婆豆腐、担担面、回锅肉、夫妻肺片等，每样都让人垂涎三尺。', \\\n",
        "    params_refine_text=params_refine_text, params_infer_code=params_infer_code)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 76
        },
        "id": "Nl_mT9KpbSrj",
        "outputId": "1bfcc06a-5246-4d25-fc19-3d125362fa59"
      },
      "outputs": [],
      "source": [
        "Audio(wav[0], rate=24_000, autoplay=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JfAba-tTbSrk"
      },
      "source": [
        "### fix random speaker"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Qh7dcWrAbSrk",
        "outputId": "3b936323-170a-496b-c4c2-6caa97a8d514"
      },
      "outputs": [],
      "source": [
        "rand_spk = chat.sample_random_speaker()\n",
        "params_infer_code = {'spk_emb' : rand_spk, }\n",
        "\n",
        "wav = chat.infer('四川美食确实以辣闻名，但也有不辣的选择。比如甜水面、赖汤圆、蛋烘糕、叶儿粑等，这些小吃口味温和，甜而不腻，也很受欢迎。', \\\n",
        "    params_refine_text=params_refine_text, params_infer_code=params_infer_code)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 76
        },
        "id": "0ljWDWzabSrk",
        "outputId": "8ade2469-c226-44ae-c3a7-ff034e2bffbf"
      },
      "outputs": [],
      "source": [
        "Audio(wav[0], rate=24_000, autoplay=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "u1q-BcUKbSrl"
      },
      "source": [
        "### Two stage control"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3hAAc0lJbSrl",
        "outputId": "8dc45586-fb2a-4e81-ee53-0ce6df2fc43a"
      },
      "outputs": [],
      "source": [
        "text = \"So we found being competitive and collaborative was a huge way of staying motivated towards our goals, so one person to call when you fall off, one person who gets you back on then one person to actually do the activity with.\"\n",
        "refined_text = chat.infer(text, refine_text_only=True)\n",
        "refined_text"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0GVJxhd3BKQX",
        "outputId": "f1484519-7130-450a-b7d8-09de5fe2ffd1"
      },
      "outputs": [],
      "source": [
        "wav = chat.infer(refined_text)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 76
        },
        "id": "ngyMht74BicY",
        "outputId": "8c7447ad-9ac7-4264-9f53-057d47d43931"
      },
      "outputs": [],
      "source": [
        "Audio(wav[0], rate=24_000, autoplay=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "R2WjuVrWbSrl",
        "outputId": "0d644cb9-4d65-4147-bd99-d5451439be02"
      },
      "outputs": [],
      "source": [
        "text = 'so we found being competitive and collaborative [uv_break] was a huge way of staying [uv_break] motivated towards our goals, [uv_break] so [uv_break] one person to call [uv_break] when you fall off, [uv_break] one person who [uv_break] gets you back [uv_break] on then [uv_break] one person [uv_break] to actually do the activity with.'\n",
        "wav = chat.infer(text, skip_refine_text=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 76
        },
        "id": "71Y4pBdl-_Yd",
        "outputId": "d44fdf1a-c9e8-42ff-ab96-8712986418fa"
      },
      "outputs": [],
      "source": [
        "Audio(wav[0], rate=24_000, autoplay=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GG5AMbQbbSrl"
      },
      "source": [
        "## LLM Call"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3rkfwc3UbSrl"
      },
      "outputs": [],
      "source": [
        "from ChatTTS.experimental.llm import llm_api\n",
        "\n",
        "API_KEY = ''\n",
        "client = llm_api(api_key=API_KEY,\n",
        "        base_url=\"https://api.deepseek.com\",\n",
        "        model=\"deepseek-chat\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TTkIsXozbSrm"
      },
      "outputs": [],
      "source": [
        "user_question = '四川有哪些好吃的美食呢?'\n",
        "text = client.call(user_question, prompt_version = 'deepseek')\n",
        "print(text)\n",
        "text = client.call(text, prompt_version = 'deepseek_TN')\n",
        "print(text)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "qNhCJG4VbSrm"
      },
      "outputs": [],
      "source": [
        "params_infer_code = {'spk_emb' : rand_spk, 'temperature':.3}\n",
        "\n",
        "wav = chat.infer(text, params_infer_code=params_infer_code)"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [
        "bAUs0rGQbSrh"
      ],
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.8"
    },
    "widgets": {
      "application/vnd.jupyter.widget-state+json": {
        "1da23fc236034f32adcaf6bb2e0e7d80": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "HTMLModel",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HTMLModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HTMLView",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_2f920c00bcac4787a0078ee035e97b43",
            "placeholder": "​",
            "style": "IPY_MODEL_ba592297ff5347aebae298770a29fb8c",
            "value": " 11/11 [00:00&lt;00:00, 762.51it/s]"
          }
        },
        "20aa0031b7bb45bf82443b48b3694166": {
          "model_module": "@jupyter-widgets/base",
          "model_module_version": "1.2.0",
          "model_name": "LayoutModel",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "2f920c00bcac4787a0078ee035e97b43": {
          "model_module": "@jupyter-widgets/base",
          "model_module_version": "1.2.0",
          "model_name": "LayoutModel",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "4b2126d97c514795ab2a90f7357a203c": {
          "model_module": "@jupyter-widgets/base",
          "model_module_version": "1.2.0",
          "model_name": "LayoutModel",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "67252ea545d64392a1bd6ac40852e65f": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "ProgressStyleModel",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "ProgressStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "bar_color": null,
            "description_width": ""
          }
        },
        "96c9bb2eff4043b2a5dbd1e3e65375e5": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "DescriptionStyleModel",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "DescriptionStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "description_width": ""
          }
        },
        "9775ce64008b417fac3edd55b9e999d9": {
          "model_module": "@jupyter-widgets/base",
          "model_module_version": "1.2.0",
          "model_name": "LayoutModel",
          "state": {
            "_model_module": "@jupyter-widgets/base",
            "_model_module_version": "1.2.0",
            "_model_name": "LayoutModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "LayoutView",
            "align_content": null,
            "align_items": null,
            "align_self": null,
            "border": null,
            "bottom": null,
            "display": null,
            "flex": null,
            "flex_flow": null,
            "grid_area": null,
            "grid_auto_columns": null,
            "grid_auto_flow": null,
            "grid_auto_rows": null,
            "grid_column": null,
            "grid_gap": null,
            "grid_row": null,
            "grid_template_areas": null,
            "grid_template_columns": null,
            "grid_template_rows": null,
            "height": null,
            "justify_content": null,
            "justify_items": null,
            "left": null,
            "margin": null,
            "max_height": null,
            "max_width": null,
            "min_height": null,
            "min_width": null,
            "object_fit": null,
            "object_position": null,
            "order": null,
            "overflow": null,
            "overflow_x": null,
            "overflow_y": null,
            "padding": null,
            "right": null,
            "top": null,
            "visibility": null,
            "width": null
          }
        },
        "ba592297ff5347aebae298770a29fb8c": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "DescriptionStyleModel",
          "state": {
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "DescriptionStyleModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/base",
            "_view_module_version": "1.2.0",
            "_view_name": "StyleView",
            "description_width": ""
          }
        },
        "c365a95346ec4b09a1e6467bf313baf7": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "HBoxModel",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HBoxModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HBoxView",
            "box_style": "",
            "children": [
              "IPY_MODEL_d79fd51849fd463cb08b83fdb8e5ca0c",
              "IPY_MODEL_d247683a0a61441b971dfb39062e1fbf",
              "IPY_MODEL_1da23fc236034f32adcaf6bb2e0e7d80"
            ],
            "layout": "IPY_MODEL_4b2126d97c514795ab2a90f7357a203c"
          }
        },
        "d247683a0a61441b971dfb39062e1fbf": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "FloatProgressModel",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "FloatProgressModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "ProgressView",
            "bar_style": "success",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_20aa0031b7bb45bf82443b48b3694166",
            "max": 11,
            "min": 0,
            "orientation": "horizontal",
            "style": "IPY_MODEL_67252ea545d64392a1bd6ac40852e65f",
            "value": 11
          }
        },
        "d79fd51849fd463cb08b83fdb8e5ca0c": {
          "model_module": "@jupyter-widgets/controls",
          "model_module_version": "1.5.0",
          "model_name": "HTMLModel",
          "state": {
            "_dom_classes": [],
            "_model_module": "@jupyter-widgets/controls",
            "_model_module_version": "1.5.0",
            "_model_name": "HTMLModel",
            "_view_count": null,
            "_view_module": "@jupyter-widgets/controls",
            "_view_module_version": "1.5.0",
            "_view_name": "HTMLView",
            "description": "",
            "description_tooltip": null,
            "layout": "IPY_MODEL_9775ce64008b417fac3edd55b9e999d9",
            "placeholder": "​",
            "style": "IPY_MODEL_96c9bb2eff4043b2a5dbd1e3e65375e5",
            "value": "Fetching 11 files: 100%"
          }
        }
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
